package com.sevennotes.classtable.ui.pages.littlepages

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.sevennotes.classtable.model.ClassAmountRecord
import com.sevennotes.classtable.model.GradePointRecord
import com.sevennotes.classtable.ui.pages.components.*
import com.sevennotes.classtable.ui.pages.main_screen.CaState
import com.sevennotes.classtable.utils.TimeUtil
import java.util.*

@Composable
fun ClassAmountPage(
    caState: CaState,
    onAdd: (ClassAmountRecord) -> Unit,
    onDelete: (ClassAmountRecord) -> Unit
) {
    val caList = caState.caChangeList?.observeAsState()?.value
    //grade point change dialog display
    var cacDialog by remember { mutableStateOf(false) }
    //on delete comfirm dialog display
    var confirmDialog by remember { mutableStateOf(false) }
    AnimatedVisibility(visible = cacDialog) {
        InfoChangeDialog(note = "", number = 0, unitText = "节", onConfirm = { note, num ->
            onAdd(
                ClassAmountRecord(
                    studentId = caState.studentId,
                    note = note,
                    amountChange = num,
                    date = Date()
                )
            )
            cacDialog = false
        }, onCancel = { cacDialog = false })
    }

    Column(modifier = Modifier.fillMaxSize()) {
        Button(
            modifier = Modifier.fillMaxWidth(),
            onClick = { cacDialog = true }
        ) {
            Text(text = "课时变更")
        }
        Spacer(modifier = Modifier.height(10.dp))
        LazyColumn {
            caList?.let { caItems ->
                items(caItems) { ca ->
                    AnimatedVisibility(visible = confirmDialog) {
                        SimpleComfirmDialog(
                            text = "确定要删除吗?",
                            onConfirmClick = {
                                onDelete(ca)
                                confirmDialog = false
                            },
                            onCancelClick = { confirmDialog = false }
                        )
                    }
                    NumberItemCell(
                        editable = caState.editable.value,
                        onDeleteClick = { confirmDialog = true },
                        date = ca.date,
                        note = ca.note,
                        changeNumber = ca.amountChange
                    )
                }
            }
        }
    }
}
